from WinningEvaluation import WinningEvaluation
from Chessnut import Game

import random
import copy

def StrGame(game):
    game = str(game)
    game = list(game)
    out = ""
    i = 0
    while True:
        if game[i] != " ":
            out = out + str(game[i])
            i += 1
        else:
            break
    return out

def boardlist(fen):
    out = ""
    fen = list(fen)
    while "1" in fen:
        fen[fen.index("1")] = " "
    while "2" in fen:
        fen[fen.index("2")] = "  "
    while "3" in fen:
        fen[fen.index("3")] = "   "
    while "4" in fen:
        fen[fen.index("4")] = "    "
    while "5" in fen:
        fen[fen.index("5")] = "     "
    while "6" in fen:
        fen[fen.index("6")] = "      "
    while "7" in fen:
        fen[fen.index("7")] = "       "
    while "8" in fen:
        fen[fen.index("8")] = "        "
    while "/" in fen:
        del fen[fen.index("/")]
    for i in fen:
        out = out + i
    return list(out)

def op(playerside):
    if playerside == "b":
        return "w"
    if playerside == "w":
        return "b"

def GiveBestMove(chessgame, playerside, deep, lastscr):
    if deep == 0:
        copygame = copy.deepcopy(chessgame)
        moves = chessgame.get_moves()
        move = random.choice(moves)
        copygame.apply_move(move)
        back = [move, WinningEvaluation(boardlist(StrGame(copygame)))]
        del copygame
        return back
    if deep == 1:
        moves = chessgame.get_moves()
        movescrs = []
        for move in moves:
            copyone = copy.deepcopy(chessgame)
            copyone.apply_move(move)
            WinningEvaluatscr = WinningEvaluation(boardlist(StrGame(copyone)))
            if playerside == "b":
                if WinningEvaluatscr < lastscr:
                    continue
            elif playerside == "w":
                if WinningEvaluatscr > lastscr:
                    continue
            movescrs.append(WinningEvaluatscr)
        i = 0
        for scr in movescrs:
            if playerside == "b":
                if scr < max(movescrs):
                    movescrs.remove(scr)
                    del moves[i]
            elif playerside == "w":
                if scr > min(movescrs):
                    movescrs.remove(scr)
                    del moves[i]
            i += 1
            
        back = [random.choice(moves), max(movescrs)]
        return back
    else:
        movesscrs = []
        nextmoves = []
        moves = chessgame.get_moves()
        for move in moves:
            copygame = copy.deepcopy(chessgame)
            copygame.apply_move(move)
            WinningEvaluatscr = WinningEvaluation(boardlist(StrGame(copygame)))
            if playerside == "b":
                if WinningEvaluatscr < lastscr:
                    continue
            elif playerside == "w":
                if WinningEvaluatscr > lastscr:
                    continue
            nextmove = GiveBestMove(copygame, op(playerside), deep-1, WinningEvaluatscr)
            movesscrs.append(nextmove[1])
            nextmoves.append(move)
        i = 0
        for scr in movesscrs:
            if playerside == "b":
                if scr < max(movesscrs):
                    movesscrs.remove(scr)
                    del nextmoves[i]
            elif playerside == "w":
                if scr > min(movesscrs):
                    movesscrs.remove(scr)
                    del nextmoves[i]
            i += 1
        
        back = [random.choice(nextmoves), max(movesscrs)]
        return back